package com.learn.ack;

import com.learn.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/08/16
 * @description ch03 快
 */
public class Ack01 {
    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.basicConsume(QUEUE_NAME, false, (consumerTag, message)->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(new String(message.getBody(), StandardCharsets.UTF_8));

            // prefetchCount 预取值 – 消费者可分发的最大消息数量 0表示无限制
            // 可用于不公平分发
            channel.basicQos(2);

            // 手动确认,获取消息的标记号，不批量确认
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        }, (consumerTag)->{
            System.out.println(consumerTag);
        });
    }
}
